#pragma once

#include <string>
#include <vector>
#include <iostream>
#include "TLV.hpp"
#include "Packet.hpp"
#include "TcpConnection.hpp"

extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
}


namespace wd{


//编码器参数
struct VideoMetadata{
int cameraId;
int codecId;
int width;
int height;
int timeBaseNum; //时间基的分子
int timeBaseDen; //时间基的分母
string spsSPS; //H.264/H.265的SPS/PPS数据
};

//视频帧数据
struct VideoFrame{
    uint32_t size; //数据长度
    int64_t pts; //时间戳
    int64_t dts; //解码时间戳(网络字节序)
    uint8_t *data; //编码数据(指向AVPacket的Data)
};

//分片结构
struct DataFragment{
    uint8_t type;
    uint8_t isFragmented;
    uint16_t totalFragments;
    uint16_t currentFragment;
    uint32_t packerId;
    uint16_t dataLen;
    char data[1380];
};

class CameraTask
{
public:
    CameraTask(Packet &packet,TcpConnectionPtr conn)
    :_packet(packet)
    ,_conn(conn)
    {}

    ~CameraTask() {}

    //发送解码器参数
    void sendMetaData(AVFormatContext*,int);

    //发送视频帧数据
    void sendPacketToClient(AVPacket*);

    //数据分片
     void fragmentAndSend(const char*,size_t,uint8_t);

     //序列化meta数据
    vector<char> serializeMetaData(VideoMetadata);

    //生成唯一id
    uint32_t generatePacketId();

    void process();

private:
    Packet _packet;
    TcpConnectionPtr _conn;

};

}//end of namespace wd
